package dwetl.model;

import java.lang.reflect.Method;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

//支持transform_func配置自定义列值转换方法，配置的方法必须是public的静态方法
public class ValTransformColumn {
    private static final Logger logger = LoggerFactory.getLogger(ValTransformColumn.class);
    private String transform_func;

    public String getTransform_func() {
        return transform_func;
    }

    public void setTransform_func(String transform_func) {
        this.transform_func = transform_func;
    }

    public Object valTransform(Object srcVal){
        String className=null;
        String method=null;
        try {
            if (this.transform_func != null && this.transform_func.length() > 0) {
                className = transform_func.substring(0, transform_func.lastIndexOf('.'));
                method = transform_func.substring(1 + transform_func.lastIndexOf('.'));
                Class<?> threadClazz = Class.forName(className);
                Method m = threadClazz.getMethod(method, Object.class);

                return (m.invoke(null, srcVal));
            }
        }catch(Exception e){
            logger.error("class:"+className+" method:"+method+" invoke failed.",e);
//            e.printStackTrace();
        }
        return srcVal;
    }
}
